﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Collections;

using System.Runtime.InteropServices;
//using Outlook = Microsoft.Office.Interop.Outlook;

using System.IO;

public partial class admin_Assignment : System.Web.UI.Page
{
    rUser ruser;
    DataSet ds;
    DataSet usuEmployee;
    string where1 = "";
   
    DataSet companies;
    DataSet accounts;
    DataSet products;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Session["userid"] == null)
        {

            Response.Redirect("../Default.aspx");
        }
        
            //mostrarPaginas();

           
            ds = null;
            
            ruser = (rUser)Session["userid"];
        if (!Page.IsPostBack)
        {
            int perfilID = int.Parse(ruser.getPerfilId());
           
            RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
            RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
            /*if ( Session["FilterSearch"]!= null)
            {
                lbtest.Text = "*" + (string)Session["FilterSearch"] + "*";
            }*/
            // si viene de singlesearch
            #region singlesearch 
            if (Session["masivo"] != null)
            {
               
                where1 = (string)Session["masivo"];
               
                ds = DB.GetOrders(permi.int64C1, permi2.int64C1, "masivo", "", ruser.getAlias(), where1, "", "", 1, perfilID);
                lnk_Assign.Enabled = true;
                Session["masivo"] = null;
                Session["wherems"] = where1;
                if (ds != null)
                {
                    gv_multiple.DataSource = ds;
                    gv_multiple.DataBind();
                    gv_multipledrv.DataSource = ds;
                    gv_multipledrv.DataBind();
                    gv_multipleqc.DataSource = ds;
                    gv_multipleqc.DataBind();
                    gv_duedate.DataSource = ds;
                    gv_duedate.DataBind();
                    ListItem other = new ListItem();
                    other.Text = "Other"; other.Value = "OT";
                    cb_driver.Items.Add(other);
                    cb_driver.Items[cb_driver.Items.Count - 1].Attributes.Add("disabled", "true");
                    ListItem fs = new ListItem();
                    fs.Text = "FS Team";
                    fs.Value = "FS";
                    cb_driver.Items.Add(fs);

                    lnk_Assign.Visible = true;
                    lnk_Assigndrv.Visible = true;
                    lnk_Assignqc.Visible = true;

                    //colocando contadores
                    lbcont2drv.Text = ds.Tables[0].Rows.Count.ToString();
                    lbcont2pro.Text = ds.Tables[0].Rows.Count.ToString();
                    lbcont2qc.Text =  ds.Tables[0].Rows.Count.ToString();
                    lbcont2due.Text = ds.Tables[0].Rows.Count.ToString();
                    
                }
            }
            else {

                #region FilteredSearch, viene de busqueda filtrada o logistica par asignar
                if (Session["FilterSearch"] != null)
                {
                    where1 = (string)Session["wherems"];
                    lbtest.Text = where1;
                ds = DB.GetOrders(permi.int64C1, permi2.int64C1, "masivo", "", ruser.getAlias(), where1, "", "", 1, perfilID);
                lnk_Assign.Enabled = true;
                Session["FilterSearch"] = null;
                Session["wherems"] = where1;
                if (ds != null)
                {
                    gv_multiple.DataSource = ds;
                    gv_multiple.DataBind();
                    gv_multipledrv.DataSource = ds;
                    gv_multipledrv.DataBind();
                    gv_multipleqc.DataSource = ds;
                    gv_multipleqc.DataBind();
                    gv_duedate.DataSource = ds;
                    gv_duedate.DataBind();
                    ListItem other = new ListItem();
                    other.Text = "Other"; other.Value = "OT";
                    cb_driver.Items.Add(other);
                    cb_driver.Items[cb_driver.Items.Count - 1].Attributes.Add("disabled", "true");
                    ListItem fs = new ListItem();
                    fs.Text = "FS Team";
                    fs.Value = "FS";
                    cb_driver.Items.Add(fs);

                    lnk_Assign.Visible = true;
                    lnk_Assigndrv.Visible = true;
                    lnk_Assignqc.Visible = true;

                    //colocando contadores
                    lbcont2drv.Text = ds.Tables[0].Rows.Count.ToString();
                    lbcont2pro.Text = ds.Tables[0].Rows.Count.ToString();
                    lbcont2qc.Text = ds.Tables[0].Rows.Count.ToString();
                    lbcont2due.Text = ds.Tables[0].Rows.Count.ToString();
                }

                }
                #endregion
                else {
                    lnk_Assign.Visible = false;
                    lnk_Assigndrv.Visible = false;
                    lnk_Assignqc.Visible = false;
                
                }
                


            }
            #endregion
           
           

            //restricted();
            //permiso();
           
       
        }
       
        lnk_Assign.Visible = true;
        lnk_Assigndrv.Visible = true;
        lnk_Assignqc.Visible = true;
        lnk_Assign.Enabled = true;
        lnk_Assigndrv.Enabled = true;
        lnk_Assignqc.Enabled = true;
       
        if (ds != null)
        {
            gv_multiple.DataSource = ds;
            gv_multiple.DataBind();
            gv_multipledrv.DataSource = ds;
            gv_multipledrv.DataBind();
            gv_multipleqc.DataSource = ds;
            gv_multipleqc.DataBind();
        }
    }

    protected void Page_Init(object sender, EventArgs e) {
        cb_processor.Items.Clear();
        cb_driver.Items.Clear();
        cb_processor.Items.Add("Select One");
        cb_driver.Items.Add("Select One");
        cb_qc.Items.Add("Select One");
        Utility.fillAgent("", 3, 2, cb_processor);
        Utility.fillAgent("", 7, 2, cb_processor);
        Utility.fillAgent("", 10, 2, cb_processor);
        Utility.fillAgent("", 13, 2, cb_processor);
        Utility.fillAgent("", 1, 1, cb_driver);
        Utility.fillAgent("", 15, 1, cb_qc);
        Utility.fillAgent("", 7, 1, cb_qc);
        Utility.fillAgent("", 10, 1, cb_qc);
        restricted();
        permiso();
    
    }

    private void permiso()
    {
         long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
           // permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 16);
            permiso = permi.int64C1;
           
        }
        if ((permiso & 2) != 2)
        {
          //  lnk_Assign.Visible = true; lnk_Assign.Enabled = false;
            
            PProc.Enabled = false;
        }
        
        if ((permiso & 4) != 4)
        {
            //lnk_Assigndrv.Visible = true; lnk_Assigndrv.Enabled = false;
            PDriver.Enabled = false;
        }
        if ((permiso & 8) != 8)
        {
            //lnk_Assignqc.Visible = true; lnk_Assignqc.Enabled = false;
            PQuality.Enabled = false;
        }
        if ((permiso & 16) != 16)
        {
            //lnk_Assignqc.Visible = true; lnk_Assignqc.Enabled = false;
            Panel1.Enabled = false;
        }
    }

    private void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {

            RE_GenericBean permi = Utility.getPermisobyRuser(user, 16);
            permiso = permi.int64C2;
        }
        if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
        if ((permiso & 2) != 2)
        {
            moveTabPanel(TabProcessor, PProc, TabContainer2, "Processor", true);
        }
      
        if ((permiso & 4) != 4)
        {
            moveTabPanel(TabDriver, PDriver, TabContainer2, "Driver", true);
        }
        if ((permiso & 8) != 8)
        {
            moveTabPanel(TabQuality, PQuality, TabContainer2, "QualityAgent", true);
        }
        if ((permiso & 16) != 16)
        {
            moveTabPanel(TabDueDate, Panel1, TabContainer2, "DueDate", true);
        }
        TabContainer1.Visible = false;
    }

    private void moveTabPanel(AjaxControlToolkit.TabPanel tabpanel1, Panel Panel, AjaxControlToolkit.TabContainer TabContainer2, string headertext, bool visible)
    {
        tabpanel1.Controls.Remove(Panel);
        AjaxControlToolkit.TabPanel tabpanel = new AjaxControlToolkit.TabPanel();
        tabpanel.ID = headertext; tabpanel.HeaderText = headertext;
        tabpanel.Controls.Add(Panel);
        TabContainer2.Tabs.Add(tabpanel);
        TabContainer2.Visible = visible;
    }


    protected void bt_searchgroup_Click(object sender, ImageClickEventArgs e)
    {
        

    }

    protected void gv_multiple_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Style.Add("display", "none");//damages
            e.Row.Cells[2].Style.Add("display", "none");//order #
            e.Row.Cells[3].Style.Add("display", "none");//duedate
            e.Row.Cells[4].Style.Add("display", "none");//duetime
            e.Row.Cells[5].Style.Add("display", "none");//client
            e.Row.Cells[6].Style.Add("display", "none");//company  
            e.Row.Cells[7].Style.Add("display", "none");//account   
            e.Row.Cells[8].Style.Add("display", "none");//type
            e.Row.Cells[12].Style.Add("display", "none");//state
            e.Row.Cells[13].Style.Add("display", "none");//county
            e.Row.Cells[14].Style.Add("display", "none");//zip
            e.Row.Cells[16].Style.Add("display", "none");//driver
            e.Row.Cells[17].Style.Add("display", "none");//qc initials
            e.Row.Cells[18].Style.Add("display", "none");//PIComments
            e.Row.Cells[19].Style.Add("display", "none");//status
            e.Row.Cells[20].Style.Add("display", "none");//apptdate
            e.Row.Cells[21].Style.Add("display", "none");//appttime
            e.Row.Cells[22].Style.Add("display", "none");//apptcomments
            e.Row.Cells[23].Style.Add("display", "none");//
            e.Row.Cells[24].Style.Add("display", "none");//
            e.Row.Cells[25].Style.Add("display", "none");//specialattributes
            e.Row.Cells[26].Style.Add("display", "none");//ord_failed 
            e.Row.Cells[27].Style.Add("display", "none");//ord_failed_id
            e.Row.Cells[28].Style.Add("display", "none");//ord_processor_done
            e.Row.Cells[29].Style.Add("display", "none");//ord_driver_done
            e.Row.Cells[30].Style.Add("display", "none");//ord_qc_done
            e.Row.Cells[31].Style.Add("display", "none");//order_id
            e.Row.Cells[32].Style.Add("display", "none");//ord_rv
            e.Row.Cells[33].Style.Add("display", "none");//indice
            e.Row.Cells[34].Style.Add("display", "none");//paid
        }

        if (e.Row.RowType == DataControlRowType.Header){
            e.Row.Cells[0].Width = 100; //order
            e.Row.Cells[9].Width = 80; //st
            e.Row.Cells[10].Width = 120; //stadres
            e.Row.Cells[11].Width = 150; //city
           // e.Row.Cells[16].Width = 150;
            e.Row.Cells[15].Text = "Processor"; //proc
            //e.Row.Cells[16].Text = "Driver";
            e.Row.Cells[15].Width = 200;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Left;
           // e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Left;
            
        
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            e.Row.Cells[15].Width = 150;
           // e.Row.Cells[16].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Left;
           // e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Left;
            string check = "";
            //escribiendo nombre usuario
            usuEmployee= (DataSet)Session["Usuarios"];
            if (usuEmployee!=null){
                for (int i = 0; i < usuEmployee.Tables[0].Rows.Count; i++)
                {
                    if (e.Row.Cells[15].Text.ToUpper().Equals(usuEmployee.Tables[0].Rows[i]["alias"].ToString().ToUpper()))
                    {
                        if (e.Row.Cells[28].Text.Trim() == "YES")
                        { check = "<img src=\"../img/checkdone.png \" align=\"left\" >";}
                        else { check = ""; }
                        e.Row.Cells[15].Text = check + usuEmployee.Tables[0].Rows[i]["name"].ToString().ToUpper() + " " + usuEmployee.Tables[0].Rows[i]["lastname"].ToString().ToUpper();
                    }
                  
                }
                if (e.Row.Cells[15].Text.ToUpper().Equals("CXL"))
                {
                    /*if (e.Row.Cells[28].Text.Trim() == "YES")
                    { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                    else { check = ""; }*/
                    e.Row.Cells[15].Text = check + "CANCELLED";
                }
               
            }
            


        }
    }

    private int sendMail(string body, string valor)
    {
        int result = 0;
        //buscando email del procesador
        DataSet usuEmployee;
        string mail = "";
        usuEmployee = (DataSet)HttpContext.Current.Session["Usuarios"];
        if (usuEmployee == null)
        {
            setVariablesSession();
        }
        if (usuEmployee != null)
        {
            for (int i = 0; i < usuEmployee.Tables[0].Rows.Count; i++)
            {


                string alias = usuEmployee.Tables["Tabla"].Rows[i]["alias"].ToString().ToUpper().Trim();
                if (alias.Equals(valor.ToUpper().Trim()))
                {
                    mail = usuEmployee.Tables["Tabla"].Rows[i]["email"].ToString().Trim();
                }

            }



            string from = "bpo@bpophotos.com";
            string Subject = "New order Assigned in bpophotos.com";

            /*// string res = Utility.sendMail(from, mail, Subject, body);
             // Create the Outlook application by using inline initialization.
             Outlook.Application oApp = new Outlook.Application();

             //Create the new message by using the simplest approach.
             Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
        

             //Add a recipient.
             // TODO: Change the following recipient where appropriate.
             Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add(mail);
     
             oRecip.Resolve();

             //Set the basic properties.
             oMsg.Subject = Subject;
             //oMsg.Body = body;
             oMsg.HTMLBody = body;

             //Add an attachment.
             /*TODO: change file path where appropriate
             String sSource = "C:\\setupxlg.txt";
             String sDisplayName = "MyFirstAttachment";
             int iPosition = (int)oMsg.Body.Length + 1;
             int iAttachType = (int)Outlook.OlAttachmentType.olByValue;  
             Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);
         */
            /*   // If you want to, display the message.
               oMsg.Display(true);  //modal

               //Send the message.
               //oMsg.Save();
               //oMsg.Send();

               //Explicitly release objects.
               oRecip = null;
               //oAttach = null;
               oMsg = null;
               oApp = null;*/

            result = 1;
        }
        return result;         
    }
    protected void lnk_search_Click(object sender, EventArgs e)
    {
        TabContainer1.Visible = true;
        string ordersgroup = tb_orders.Text.Trim();

        ordersgroup = ordersgroup.Replace(",", "','");
        ordersgroup = "'" + ordersgroup + "'";
        ordersgroup = "and orden.ordernum in (" + ordersgroup + ")";
        if (!cb_msubmitted.SelectedValue.Equals("All"))
        {

            if (cb_msubmitted.SelectedValue.ToUpper().Equals("NO"))
            {
                ordersgroup = ordersgroup + " and (orden.ord_submit = 'NO' or orden.ord_submit is null )";
            }
            else
            {
                ordersgroup = ordersgroup + " and orden.ord_submit = 'YES'";
            }

        }
        Session["masivo"] = ordersgroup;
        Session["wherems"] = ordersgroup;
        Response.Redirect("Assignment.aspx");
    }

    protected void lnk_Assign_Click(object sender, EventArgs e)
    {
        
        setVariablesSession();
        lb_error.Visible = false; lb_mensaje.Visible = false;
        string ordersgroup = ""; int p = 0; int pos = 0;
        string[] orders = new string[100];
        foreach (GridViewRow ROW in gv_multiple.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX1");
            if (chk.Checked)
            {
                Label label = (Label)ROW.FindControl("lb_order");
                if (p == 1) { ordersgroup += " , "; } else { p = 1; }
                ordersgroup += label.Text;
                orders[pos] = label.Text.Trim();
                pos++;

            }

        }
        ordersgroup = "and orden.ordernum in (" + ordersgroup + ")";
        int resop = 0; int resdr = 0;

        string body=""; string[] name;
        if (cb_processor.SelectedIndex.ToString() != "0")
        {
            name = cb_processor.SelectedItem.ToString().Split('-');
            //  string body = "Hello  " + name[1] + " : <br><br> This is an Automated email confirming that a new order has been assigned to you. <br><table border=\"1\" cellspacing=\"0\" cellspading=\"0\">";
            // body = body + "<tr bgcolor=\"#9AC9F8\"><td>Order #:</td><td>Due Date </td><td>Due Time </td><td>Address </td><td>City </td><td>State </td><td>Zip Code </td><td>Comments </td></tr>";
            body = "Hello  " + name[1] + " : <br><br> This is an Automated email confirming that a new order has been assigned to you. <br><br><br>";
            //string ordernum = DB.getOrderNum(numero, "order_id", int.Parse(orders[i]));
            for (int i = 0; i < pos; i++)
            {
                int numero = int.Parse(orders[i].ToString());
                resop = DB.assignOrder("ord_processor", cb_processor.SelectedValue.ToUpper().Trim(), "order_id", int.Parse(orders[i]), 0, 3, "ord_processor_done");
                if (resop == 1)
                {
                    lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true;
                     
                    RE_GenericBean rgb = DB.getOrden(int.Parse(orders[i]));
                    if (rgb != null)
                    {
                        string dir = rgb.strC5 + "&nbsp;" + rgb.strC6 + "&nbsp;" + rgb.strC7 + "&nbsp;" + rgb.strC9;
                        string hora = Utility.Hora24horastoFormatoAMPM(rgb.strC3).strC1 + Utility.Hora24horastoFormatoAMPM(rgb.strC3).strC2;
                        products= (DataSet)Session["products"];
                        //cambiando el produc_id por product_name
                        if (products != null) {
                            for (int k = 0; k < products.Tables[0].Rows.Count; k++ ) {
                                if (products.Tables[0].Rows[k][0].ToString().Equals(rgb.strC1)){
                                    rgb.strC1 = products.Tables[0].Rows[k][2].ToString();
                                }
                            
                            }
                        }
                        //cambiando company_id por company name
                        companies= (DataSet)Session["companies"];
                        if (companies != null)
                        {
                            for (int k = 0; k < companies.Tables[0].Rows.Count; k++)
                            {
                                if (companies.Tables[0].Rows[k][0].ToString().Equals(rgb.strC23))
                                {
                                    rgb.strC23 = companies.Tables[0].Rows[k][1].ToString();
                                }

                            }
                        }
                        //cambiando account_id por account name
                        accounts = (DataSet)Session["accounts"];
                        if (accounts != null)
                        {
                            for (int k = 0; k < accounts.Tables[0].Rows.Count; k++)
                            {
                                if (accounts.Tables[0].Rows[k][0].ToString().Equals(rgb.strC22))
                                {
                                    rgb.strC22 = accounts.Tables[0].Rows[k][2].ToString();
                                }

                            }
                        }

                        body = body + "<span>" + rgb.strC39 + "&nbsp;&nbsp;</span><span>" + hora + "&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC23 + "&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC22 + "&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC1 + "&nbsp;&nbsp;&nbsp;</span><span><b>" + dir + "&nbsp;&nbsp;&nbsp;</b></span><span>"  + rgb.strC4 + "     </span><br>";
                    }

                }
                else if (resop == 2) { lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true; }
            }
            int r = sendMail(body, cb_processor.SelectedValue.ToUpper().Trim());
            //enviando correo a driver outlook
           // CreateEmailMessage(cb_processor.SelectedValue.ToUpper().Trim(), "Driver Assign", "astrid.bolanos@gmail.com", body);
            if (r == 0)
            {
                lb_error.Text = "An error occurred  sending the email notification."; lb_error.Visible = true;
            }
        }
        
        
        where1 = (string)Session["wherems"];
        int perfilID = int.Parse(ruser.getPerfilId());
        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
        ds = DB.GetOrders(permi.int64C1, permi2.int64C1, "masivo", "", ruser.getAlias(), where1, "", "", 1, perfilID);

        gv_multiple.DataSource = ds;
        gv_multiple.DataBind();
        //TabContainer1.ActiveTabIndex = 0;
        //Response.Write("<script type='text/javascript'>window.open('changeGroup.aspx?get="+ordersgroup+"','cal','width=600,height=350,left=100,top=100,scrollbars=NO,resizable=NO');</script>"); 
    }

    

    private void setVariablesSession()
    {
        string rxmlcompanies = null;
        DataSet usuGeneral;
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
       

        try
        {
            companies = (DataSet)Session["companies"];
            accounts = (DataSet)Session["accounts"];
            products = (DataSet)Session["products"];
            usuEmployee= (DataSet)Session["Usuarios"];
            if (products == null) { products = (DataSet)DB.GetProducts(); }
            if (companies == null){rxmlcompanies = service.getCompanies();
            companies = Utility.getCompanies(rxmlcompanies);}
            if (accounts == null){
                 string xmlaccount = service.getAccounts();
                accounts = Utility.getAccounts(xmlaccount);
            }
            if (usuEmployee== null){
                string rxml1 = service.getPerfiles(1,1);
                DataSet perfiles = Utility.getPerfiles(rxml1);
                Session["Perfiles"] = perfiles;
                // Utility.writeError("Error Default " + rxml1);
                usuEmployee = Utility.getTodosUsuarios(rxml1);
            }
            
            Session["companies"]= companies;
            Session["accounts"] = accounts;
            Session["products"] = products;
            Session["Usuarios"] = usuEmployee;
           

        }
        catch (Exception ex) { Utility.writeError(ex.Message); }
        finally
        {
            if (service != null) { service.Dispose(); }

        }
    }


    private void CreateEmailMessage(string strTo, string strSubject, string strCC, string strBody)
    {
        try
        {
            // Create the Outlook application by using inline initialization.
            /*Outlook.Application oApp = new Outlook.Application();

            //Create the new message by using the simplest approach.
            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

            //Add a recipient.
            // TODO: Change the following recipient where appropriate.
            Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("e-mail address");
            oRecip.Resolve();

            //Set the basic properties.
            oMsg.Subject = "This is the subject of the test message";
            oMsg.Body = ".";

            //Add an attachment.
            /*TODO: change file path where appropriate
            String sSource = "C:\\setupxlg.txt";
            String sDisplayName = "MyFirstAttachment";
            int iPosition = (int)oMsg.Body.Length + 1;
            int iAttachType = (int)Outlook.OlAttachmentType.olByValue;  
            Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);
        */
            // If you want to, display the message.
        /*    oMsg.Display(true);  //modal

            //Send the message.
            //oMsg.Save();
            //oMsg.Send();

            //Explicitly release objects.
            oRecip = null;
            //oAttach = null;
            oMsg = null;
            oApp = null;*/
        }

               // Simple error handler.
        catch (Exception ex)
        {
            Response.Write("<script> alert('Email Fail ');</script>");
        }




    }
    protected void lnk_Assignqc_Click(object sender, EventArgs e)
    {
        setVariablesSession();
        lb_error.Visible = false; lb_mensaje.Visible = false;
        string ordersgroup = ""; int p = 0; int pos = 0;
        string[] orders = new string[100];
        foreach (GridViewRow ROW in gv_multipleqc.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
            if (chk.Checked)
            {
                Label label = (Label)ROW.FindControl("lb_order3");
                if (p == 1) { ordersgroup += " , "; } else { p = 1; }
                ordersgroup += label.Text;
                orders[pos] = label.Text.Trim();
                pos++;

            }

        }
        ordersgroup = "and orden.ordernum in (" + ordersgroup + ")";
        int resop = 0; int resdr = 0;

        string body = ""; string[] name;
        if (cb_qc.SelectedIndex.ToString() != "0" && cb_qc.SelectedIndex!=-1)
        {
            name = cb_qc.SelectedItem.ToString().Split('-');
            //  string body = "Hello  " + name[1] + " : <br><br> This is an Automated email confirming that a new order has been assigned to you. <br><table border=\"1\" cellspacing=\"0\" cellspading=\"0\">";
            // body = body + "<tr bgcolor=\"#9AC9F8\"><td>Order #:</td><td>Due Date </td><td>Due Time </td><td>Address </td><td>City </td><td>State </td><td>Zip Code </td><td>Comments </td></tr>";
            body = "Hello  " + name[1] + " : <br><br> This is an Automated email confirming that a new order has been assigned to you. <br><br><br>";
            //string ordernum = DB.getOrderNum(numero, "order_id", int.Parse(orders[i]));
            for (int i = 0; i < pos; i++)
            {
                int numero = int.Parse(orders[i].ToString());
                resop = DB.assignOrder("ord_qc", cb_qc.SelectedValue.ToUpper().Trim(), "order_id", int.Parse(orders[i]), 0, 3, "ord_qc_done");
                if (resop == 1)
                {
                    lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true;

                    RE_GenericBean rgb = DB.getOrden(int.Parse(orders[i]));
                    if (rgb != null)
                    {
                        string dir = rgb.strC5 + "&nbsp;" + rgb.strC6 + "&nbsp;" + rgb.strC7 + "&nbsp;" + rgb.strC9;
                        string hora = Utility.Hora24horastoFormatoAMPM(rgb.strC3).strC1 + Utility.Hora24horastoFormatoAMPM(rgb.strC3).strC2;
                        products = (DataSet)Session["products"];
                        //cambiando el produc_id por product_name
                        if (products != null)
                        {
                            for (int k = 0; k < products.Tables[0].Rows.Count; k++)
                            {
                                if (products.Tables[0].Rows[k][0].ToString().Equals(rgb.strC1))
                                {
                                    rgb.strC1 = products.Tables[0].Rows[k][2].ToString();
                                }

                            }
                        }
                        //cambiando company_id por company name
                        companies = (DataSet)Session["companies"];
                        if (companies != null)
                        {
                            for (int k = 0; k < companies.Tables[0].Rows.Count; k++)
                            {
                                if (companies.Tables[0].Rows[k][0].ToString().Equals(rgb.strC23))
                                {
                                    rgb.strC23 = companies.Tables[0].Rows[k][1].ToString();
                                }

                            }
                        }
                        //cambiando account_id por account name
                        accounts = (DataSet)Session["accounts"];
                        if (accounts != null)
                        {
                            for (int k = 0; k < accounts.Tables[0].Rows.Count; k++)
                            {
                                if (accounts.Tables[0].Rows[k][0].ToString().Equals(rgb.strC22))
                                {
                                    rgb.strC22 = accounts.Tables[0].Rows[k][2].ToString();
                                }

                            }
                        }

                        body = body + "<span>" + rgb.strC39 + "&nbsp;&nbsp;</span><span>" + hora + "&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC23 + "&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC22 + "&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC1 + "&nbsp;&nbsp;&nbsp;</span><span><b>" + dir + "&nbsp;&nbsp;&nbsp;</b></span><span>" + rgb.strC4 + "     </span><br>";
                    }

                }
                else if (resop == 2) { lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true; }
                else
                {
                    cb_qc.SelectedIndex = 0;
                }
            }
            int r = sendMail(body, cb_qc.SelectedValue.ToUpper().Trim());
            //enviando correo a driver outlook
            // CreateEmailMessage(cb_processor.SelectedValue.ToUpper().Trim(), "Driver Assign", "astrid.bolanos@gmail.com", body);
            if (r == 0)
            {
                lb_error.Text = "An error occurred  sending the email notification."; lb_error.Visible = true;
            }
        }

       
        where1 = (string)Session["wherems"];
        int perfilID = int.Parse(ruser.getPerfilId());
        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
        ds = DB.GetOrders(permi.int64C1, permi2.int64C1, "masivo", "", ruser.getAlias(), where1, "", "", 1, perfilID);
        
       
        gv_multipleqc.DataSource = ds;
        gv_multipleqc.DataBind();
        
    }
    protected void lnk_Assigndrv_Click(object sender, EventArgs e)
    {
        setVariablesSession();
        lb_error.Visible = false; lb_mensaje.Visible = false;
        string ordersgroup = ""; int p = 0; int pos = 0;
        string[] orders = new string[100];
        foreach (GridViewRow ROW in gv_multipledrv.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX2");
            if (chk.Checked)
            {
                Label label = (Label)ROW.FindControl("lb_order1");
                if (p == 1) { ordersgroup += " , "; } else { p = 1; }
                ordersgroup += label.Text;
                orders[pos] = label.Text.Trim();
                pos++;

            }

        }
        ordersgroup = "and orden.ordernum in (" + ordersgroup + ")";
        int resop = 0; int resdr = 0;

        string body = ""; string[] name;
      

        if (cb_driver.SelectedIndex != 0)
        {
            name = cb_driver.SelectedItem.ToString().Split('-');
            // body = "Hello  " + name[1]+ " : <br><br> This is an Automated email confirming that a new order has been assigned to you. <br><table border=\"1\" cellspacing=\"0\" cellspading=\"0\" >";
            // body = body + "<tr bgcolor=\"#9AC9F8\"><td>Order #:</td><td>Due Date </td><td>Due Time </td><td>Address </td><td>City </td><td>State </td><td>Zip Code </td><td>Comments </td></tr>";
            body = "Hello  " + name[1] + " : <br><br> This is an Automated email confirming that a new order has been assigned to you. <br><br><br>";

            //string ordernum = DB.getOrderNum(numero, "order_id", int.Parse(orders[i]));
            for (int i = 0; i < pos; i++)
            {
                int numero = int.Parse(orders[i].ToString());
                resop = DB.assignOrder("ord_driver", cb_driver.SelectedValue.ToUpper().Trim(), "order_id", int.Parse(orders[i]), 0, 2, "ord_driver_done");
                if (resop == 1)
                {
                    lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true;

                    RE_GenericBean rgb = DB.getOrden(int.Parse(orders[i]));
                    if (rgb != null)
                    {
                        string dir = rgb.strC5 + "&nbsp;" + rgb.strC6 + "&nbsp;" + rgb.strC7 + "&nbsp;" + rgb.strC9; ;
                        if (rgb.strC1.Equals("1") || rgb.strC1.Equals("6") || rgb.strC1.Equals("9")) { rgb.strC1 = "Photos Exterior"; }
                        if (rgb.strC1.Equals("2") || rgb.strC1.Equals("7")) { rgb.strC1 = "Photos Interior"; }
                        string hora = Utility.Hora24horastoFormatoAMPM(rgb.strC3).strC1 + Utility.Hora24horastoFormatoAMPM(rgb.strC3).strC2;
                        body = body + "<span><b>" + rgb.strC39 + "</b>&nbsp;&nbsp;&nbsp;</span><span>" + hora + "&nbsp;&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC1 + "&nbsp;&nbsp;&nbsp;&nbsp;</span><span><b>" + dir + "&nbsp;&nbsp;</b></span><span>" + rgb.strC30 + "&nbsp;<b>" + rgb.strC44 + "</b>&nbsp;&nbsp;</span><span><b><u>" + rgb.strC40 + "</u></b>&nbsp;&nbsp;&nbsp;</span><span>" + rgb.strC28 + "&nbsp;&nbsp;&nbsp;</span><br>";
                    }

                }
                else if (resop == 2) { lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true; }
            }
            int r = sendMail(body, cb_driver.SelectedValue.ToUpper().Trim());
            if (r == 0)
            {
                lb_error.Text = "An error occurred  sending the email notification."; lb_error.Visible = true;
            }
        }
        where1 = (string)Session["wherems"];
        int perfilID = int.Parse(ruser.getPerfilId());
        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
        ds = DB.GetOrders(permi.int64C1, permi2.int64C1, "masivo", "", ruser.getAlias(), where1, "", "", 1, perfilID);
    
        gv_multipledrv.DataSource = ds;
        gv_multipledrv.DataBind();
       // TabContainer1.ActiveTabIndex = 1;
    }
    protected void gv_multipledrv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Style.Add("display", "none");//damages
            e.Row.Cells[2].Style.Add("display", "none");//order #
            e.Row.Cells[3].Style.Add("display", "none");//duedate
            e.Row.Cells[4].Style.Add("display", "none");//duetime
            e.Row.Cells[5].Style.Add("display", "none");//client
            e.Row.Cells[6].Style.Add("display", "none");//company  
            e.Row.Cells[7].Style.Add("display", "none");//account   
            e.Row.Cells[8].Style.Add("display", "none");//type
            e.Row.Cells[12].Style.Add("display", "none");//state
            e.Row.Cells[13].Style.Add("display", "none");//county
            e.Row.Cells[14].Style.Add("display", "none");//zip
            e.Row.Cells[15].Style.Add("display", "none");//processor
            e.Row.Cells[17].Style.Add("display", "none");//qc initials
            e.Row.Cells[18].Style.Add("display", "none");//PIComments
            e.Row.Cells[19].Style.Add("display", "none");//status
            e.Row.Cells[20].Style.Add("display", "none");//apptdate
            e.Row.Cells[21].Style.Add("display", "none");//appttime
            e.Row.Cells[22].Style.Add("display", "none");//apptcomments
            e.Row.Cells[23].Style.Add("display", "none");//
            e.Row.Cells[24].Style.Add("display", "none");//
            e.Row.Cells[25].Style.Add("display", "none");//specialattributes
            e.Row.Cells[26].Style.Add("display", "none");//ord_failed 
            e.Row.Cells[27].Style.Add("display", "none");//ord_failed_id
            e.Row.Cells[28].Style.Add("display", "none");//ord_processor_done
            e.Row.Cells[29].Style.Add("display", "none");//ord_driver_done
            e.Row.Cells[30].Style.Add("display", "none");//ord_qc_done
            e.Row.Cells[31].Style.Add("display", "none");//order_id
            e.Row.Cells[32].Style.Add("display", "none");//ord_rv
            e.Row.Cells[33].Style.Add("display", "none");//indice
            e.Row.Cells[34].Style.Add("display", "none");//paid
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            //e.Row.Cells[16].Width = 150;
            //e.Row.Cells[15].Text = "Processor";
            e.Row.Cells[16].Text = "Driver";
            e.Row.Cells[15].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
           // e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Left;


        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            //e.Row.Cells[15].Width = 150;
            e.Row.Cells[16].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
           // e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Left;
             e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Left;
            string check = "";
            
         
            DataSet usuVendor = (DataSet)Session["usuVendor"];
            if (usuVendor != null)
            {
                //ahora nombre driver
                for (int i = 0; i < usuVendor.Tables[0].Rows.Count; i++)
                {
                    if (e.Row.Cells[16].Text.ToUpper().Equals(usuVendor.Tables[0].Rows[i]["alias"].ToString().ToUpper()))
                    {
                        if (e.Row.Cells[29].Text.Trim() == "YES")
                        { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                        else { check = ""; }
                        e.Row.Cells[16].Text = check + usuVendor.Tables[0].Rows[i]["name"].ToString().ToUpper() + " " + usuVendor.Tables[0].Rows[i]["lastname"].ToString().ToUpper();
                    }
                }

                if (e.Row.Cells[16].Text.ToUpper().Equals("FS"))
                {
                    if (e.Row.Cells[29].Text.Trim() == "YES")
                    { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                    else { check = ""; }
                    e.Row.Cells[16].Text = check + "FS Team";
                }
                if (e.Row.Cells[16].Text.ToUpper().Equals("CXL"))
                {
                    /*if (e.Row.Cells[29].Text.Trim() == "YES")
                    { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                    else { check = ""; }*/
                    e.Row.Cells[16].Text = check + "CANCELLED";
                }

            }


        }
    }
    protected void gv_multipleqc_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Style.Add("display", "none");//damages
            e.Row.Cells[2].Style.Add("display", "none");//order #
            e.Row.Cells[3].Style.Add("display", "none");//duedate
            e.Row.Cells[4].Style.Add("display", "none");//duetime
            e.Row.Cells[5].Style.Add("display", "none");//client
            e.Row.Cells[6].Style.Add("display", "none");//company  
            e.Row.Cells[7].Style.Add("display", "none");//account   
            e.Row.Cells[8].Style.Add("display", "none");//type
            e.Row.Cells[12].Style.Add("display", "none");//state
            e.Row.Cells[13].Style.Add("display", "none");//county
            e.Row.Cells[14].Style.Add("display", "none");//zip
            e.Row.Cells[15].Style.Add("display", "none");//processor
            e.Row.Cells[16].Style.Add("display", "none");//driver
            e.Row.Cells[18].Style.Add("display", "none");//PIComments
            e.Row.Cells[19].Style.Add("display", "none");//status
            e.Row.Cells[20].Style.Add("display", "none");//apptdate
            e.Row.Cells[21].Style.Add("display", "none");//appttime
            e.Row.Cells[22].Style.Add("display", "none");//apptcomments
            e.Row.Cells[23].Style.Add("display", "none");//
            e.Row.Cells[24].Style.Add("display", "none");//
            e.Row.Cells[25].Style.Add("display", "none");//specialattributes
            e.Row.Cells[26].Style.Add("display", "none");//ord_failed 
            e.Row.Cells[27].Style.Add("display", "none");//ord_failed_id
            e.Row.Cells[28].Style.Add("display", "none");//ord_processor_done
            e.Row.Cells[29].Style.Add("display", "none");//ord_driver_done
            e.Row.Cells[30].Style.Add("display", "none");//ord_qc_done
            e.Row.Cells[31].Style.Add("display", "none");//order_id
            e.Row.Cells[32].Style.Add("display", "none");//ord_rv
            e.Row.Cells[33].Style.Add("display", "none");//indice
            e.Row.Cells[34].Style.Add("display", "none");//paid
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            e.Row.Cells[17].Width = 150;
            e.Row.Cells[17].Text = "Quality Agent";
            //e.Row.Cells[16].Text = "Driver";
            e.Row.Cells[15].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
           // e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[17].HorizontalAlign = HorizontalAlign.Left;


        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            e.Row.Cells[17].Width = 150;
            // e.Row.Cells[16].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[17].HorizontalAlign = HorizontalAlign.Left;
            // e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Left;
            string check = "";
            //escribiendo nombre usuario
            usuEmployee = (DataSet)Session["Usuarios"];
            if (usuEmployee != null)
            {
                for (int i = 0; i < usuEmployee.Tables[0].Rows.Count; i++)
                {
                    if (e.Row.Cells[17].Text.ToUpper().Equals(usuEmployee.Tables[0].Rows[i]["alias"].ToString().ToUpper()))
                    {
                        if (e.Row.Cells[30].Text.Trim() == "YES")
                        { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                        else { check = ""; }
                        e.Row.Cells[17].Text = check + usuEmployee.Tables[0].Rows[i]["name"].ToString().ToUpper() + " " + usuEmployee.Tables[0].Rows[i]["lastname"].ToString().ToUpper();
                    }

                }
                if (e.Row.Cells[17].Text.ToUpper().Equals("CXL"))
                {
                    /*if (e.Row.Cells[28].Text.Trim() == "YES")
                    { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                    else { check = ""; }*/
                    e.Row.Cells[17].Text = check + "CANCELLED";
                }

            }
           


        }
    }


    protected void gv_duedate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Style.Add("display", "none");//damages
            e.Row.Cells[2].Style.Add("display", "none");//order #
            //e.Row.Cells[3].Style.Add("display", "none");//duedate
            e.Row.Cells[4].Style.Add("display", "none");//duetime
            e.Row.Cells[5].Style.Add("display", "none");//client
            e.Row.Cells[6].Style.Add("display", "none");//company  
            e.Row.Cells[7].Style.Add("display", "none");//account   
            e.Row.Cells[8].Style.Add("display", "none");//type
            e.Row.Cells[12].Style.Add("display", "none");//state
            e.Row.Cells[13].Style.Add("display", "none");//county
            e.Row.Cells[14].Style.Add("display", "none");//zip
            e.Row.Cells[15].Style.Add("display", "none");//processor
            e.Row.Cells[16].Style.Add("display", "none");//driver
            e.Row.Cells[17].Style.Add("display", "none");//qc
            e.Row.Cells[18].Style.Add("display", "none");//PIComments
            e.Row.Cells[19].Style.Add("display", "none");//status
            e.Row.Cells[20].Style.Add("display", "none");//apptdate
            e.Row.Cells[21].Style.Add("display", "none");//appttime
            e.Row.Cells[22].Style.Add("display", "none");//apptcomments
            e.Row.Cells[23].Style.Add("display", "none");//
            e.Row.Cells[24].Style.Add("display", "none");//
            e.Row.Cells[25].Style.Add("display", "none");//specialattributes
            e.Row.Cells[26].Style.Add("display", "none");//ord_failed 
            e.Row.Cells[27].Style.Add("display", "none");//ord_failed_id
            e.Row.Cells[28].Style.Add("display", "none");//ord_processor_done
            e.Row.Cells[29].Style.Add("display", "none");//ord_driver_done
            e.Row.Cells[30].Style.Add("display", "none");//ord_qc_done
            e.Row.Cells[31].Style.Add("display", "none");//order_id
            e.Row.Cells[32].Style.Add("display", "none");//ord_rv
            e.Row.Cells[33].Style.Add("display", "none");//indice
            e.Row.Cells[34].Style.Add("display", "none");//paid
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            e.Row.Cells[3].Width = 150;
            e.Row.Cells[3].Text = "Due Date";
            //e.Row.Cells[16].Text = "Driver";
            e.Row.Cells[15].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
           // e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;


        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[9].Width = 80;
            e.Row.Cells[10].Width = 120;
            e.Row.Cells[11].Width = 150;
            e.Row.Cells[3].Width = 150;
            // e.Row.Cells[16].Width = 150;
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;
            // e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Left;
            Label tb = new Label();
            tb.ID = "tbduedate";
            tb.Text = e.Row.Cells[3].Text;
           
            e.Row.Cells[3].Controls.Add(tb);
           
            string check = "";
            //escribiendo nombre usuario
            usuEmployee = (DataSet)Session["Usuarios"];
            if (usuEmployee != null)
            {
                for (int i = 0; i < usuEmployee.Tables[0].Rows.Count; i++)
                {
                    if (e.Row.Cells[17].Text.ToUpper().Equals(usuEmployee.Tables[0].Rows[i]["alias"].ToString().ToUpper()))
                    {
                        if (e.Row.Cells[30].Text.Trim() == "YES")
                        { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                        else { check = ""; }
                        e.Row.Cells[17].Text = check + usuEmployee.Tables[0].Rows[i]["name"].ToString().ToUpper() + " " + usuEmployee.Tables[0].Rows[i]["lastname"].ToString().ToUpper();
                    }

                }
                if (e.Row.Cells[17].Text.ToUpper().Equals("CXL"))
                {
                    /*if (e.Row.Cells[28].Text.Trim() == "YES")
                    { check = "<img src=\"../img/checkdone.png \" align=\"left\" >"; }
                    else { check = ""; }*/
                    e.Row.Cells[17].Text = check + "CANCELLED";
                }

            }
           


        }
    
    }
    protected void ln_changedue_Click(object sender, EventArgs e)
    {
        setVariablesSession();
        ArrayList arr = new ArrayList();
        lb_error.Visible = false; lb_mensaje.Visible = false;
        string ordersgroup = ""; int p = 0; int pos = 0;
        string[] orders = new string[100];
        foreach (GridViewRow ROW in gv_duedate.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX4");
            if (chk.Checked)
            {
                Label label = (Label)ROW.FindControl("lb_order4");
                if (p == 1) { ordersgroup += " , "; } else { p = 1; }
                ordersgroup += label.Text;
                orders[pos] = label.Text.Trim();
                arr.Add(int.Parse(label.Text.Trim()));
                pos++;

            }

        }
     
        int resop = 0; int resdr = 0;

        string body = ""; string[] name;


        if (!tb_newdue.Text.Equals(""))
        {
           

           
                resop = DB.changeDuedateMasivo(arr,tb_newdue.Text);
                if (resop == 1)
                {
                    lb_mensaje.Text = "Assign is done"; lb_mensaje.Visible = true;

                   

                }
               
            
            
        }
        where1 = (string)Session["wherems"];
        int perfilID = int.Parse(ruser.getPerfilId());
        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
        ds = DB.GetOrders(permi.int64C1, permi2.int64C1, "masivo", "", ruser.getAlias(), where1, "", "", 1, perfilID);

        gv_duedate.DataSource = ds;
        gv_duedate.DataBind();
        // TabContainer1.ActiveTabIndex = 1;
    }
    protected void CHDRV_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow ROW in gv_multipledrv.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX2");
            chk.Checked = CHDRV.Checked;

        }
        contar();
    }
    protected void CHPRO_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow ROW in gv_multiple.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX1");
            chk.Checked = CHPRO.Checked;

        }
        contar();
    }
    protected void CHQC_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow ROW in gv_multipleqc.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
            chk.Checked = CHQC.Checked;

        }
        contar();
    }
    protected void CHDUE_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow ROW in gv_duedate.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX4");
            chk.Checked = CHDUE.Checked;

        }
        contar();
    }
    protected void contar() {

        int sumadr = 0; int sumaproc = 0; int sumaqc = 0; int sumadue = 0;

        foreach (GridViewRow ROW in gv_multiple.Rows)
        {
            CheckBox chk = (CheckBox)ROW.FindControl("CHBX1");
            if (chk != null && chk.Checked)
            {
                sumaproc++;

            }
        }

        foreach (GridViewRow ROW in gv_multipledrv.Rows)
        {
            CheckBox chk = (CheckBox)ROW.FindControl("CHBX2");
            if (chk != null && chk.Checked)
            {
                sumadr++;

            }
        }
        foreach (GridViewRow ROW in gv_multipleqc.Rows)
        {
            CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
            if (chk != null && chk.Checked)
            {
                sumaqc++;

            }
        }
        foreach (GridViewRow ROW in gv_duedate.Rows)
        {
            CheckBox chk = (CheckBox)ROW.FindControl("CHBX4");
            if (chk != null && chk.Checked)
            {
                sumadue++;

            }
        }


        lbcont1due.Text = sumadue.ToString();
        lbcont1drv.Text = sumadr.ToString();
        lbcont1qc.Text = sumaqc.ToString();
        lbcont1pro.Text = sumaproc.ToString();
    
    }

    protected void ch_select_CheckedChanged(object sender, EventArgs e)
    {
        contar();
    }
}